package com.example.waimaiandroid.addAddress;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.ComponentName;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.baidu.mapapi.SDKInitializer;
import com.baidu.mapapi.map.MapView;
import com.example.waimai.HttpUtil.MyPost;
import com.example.waimai.context.UserInfoContext;
import com.example.waimai.pojo.UserAddress;
import com.example.waimai.util.MyThreadPool;
import com.example.waimaiandroid.R;
import com.example.waimaiandroid.address.AddressActivity;
import com.example.waimaiandroid.map.ZdpMapView;

import java.util.ArrayList;

public class addAddress extends AppCompatActivity {

    //==================================================
    // 普通控件
    private Button selectAddr; // 选择收货地址
    private RelativeLayout showAddr; // 显示收货地址
    private TextView addrAddress;
    private Button addrUpdateAddr; // 修改收货地址
    private EditText addrBottom; // 详细地址
    private EditText receiveName; // 收货人姓名
    private EditText receiveTele;// 收货人电话号码
    private Button saveAddress; // 保存地址按钮
    //==================================================
    /*百度地图控件*/
    private MapView mMapView;

    //==================================================
    // 信息
    private UserAddress userAddress;
    private int position;

    private String fromActivity = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        SDKInitializer.initialize(getApplication());
        setContentView(R.layout.activity_add_address);
        position = getIntent().getIntExtra("position",-1);
        if(position != -1){
            fromActivity = getIntent().getStringExtra("fromActivity");
            userAddress = (UserAddress) getIntent().getExtras().getSerializable("userAddress");
        }
        initView();
        initListener();
    }
    // 初始化控件
    private void initView(){
         selectAddr =  findViewById(R.id.selectAddr);
         showAddr =  findViewById(R.id.showAddr);
         addrAddress  =  findViewById(R.id.addrAddress);
         addrUpdateAddr =  findViewById(R.id.addrUpdateAddr);
         addrBottom =  findViewById(R.id.addrBottom);
         receiveName =  findViewById(R.id.receiveName);
         receiveTele =  findViewById(R.id.receiveTele);
         saveAddress =  findViewById(R.id.saveAddress);
         mMapView =  findViewById(R.id.addrBaiduMap);
         receiveTele.setText(UserInfoContext.mUser.getTelephone());
         if(fromActivity.equals("updateAddress")){
             saveAddress.setText("修改地址");
             // 传入地址信息
             addrBottom.setText(userAddress.getBottom());
             addrAddress.setText(userAddress.getAddress());
             receiveName.setText(userAddress.getReceiveName());
             receiveTele.setText(userAddress.getTelephone());
             showAddr();
         }
    }

    // 当从支付订单页面跳转过来的时候，设置的监听器是不同的，需要对 每一项都设置一个监听器

    // 初始化监听器
    private void initListener(){
        View.OnClickListener listener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 跳转到 MapView 并希望带回一个返回值
                Intent intent = new Intent();
                intent.setComponent(new ComponentName(addAddress.this, ZdpMapView.class));
                intent.putExtra("fromActivity","addAddress");
                addAddress.this.startActivityForResult(intent,1);
            }
        };
        // 选择收货地址
        selectAddr.setOnClickListener(listener);
        // 修改收货地址
        addrUpdateAddr.setOnClickListener(listener);
        // 保存地址
        saveAddress.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String bottom = addrBottom.getText().toString();
                String name = receiveName.getText().toString();
                String tele = receiveTele.getText().toString();
                /*
                * 判断是否为空
                * */
                if(bottom == null || "".equals(bottom)){
                    Toast.makeText(addAddress.this,"请输入详细地址",Toast.LENGTH_SHORT).show();
                    return ;
                }
                if(name == null || "".equals(name)){
                    Toast.makeText(addAddress.this,"请输入收货人姓名",Toast.LENGTH_SHORT).show();
                    return ;
                }
                if(tele == null || "".equals(tele)){
                    Toast.makeText(addAddress.this,"请输入收货人手机号码",Toast.LENGTH_SHORT).show();
                    return ;
                }
                userAddress.setTelephone(tele);
                userAddress.setBottom(bottom);
                userAddress.setReceiveName(name);
                userAddress.setStatus(1);
                MyThreadPool.doJob(new Runnable() {
                    @Override
                    public void run() {
                        if(fromActivity.equals("updateAddress")){
                            Boolean isSuccess = (Boolean) MyPost.doPost("updateUserAddress",userAddress);
                            if(isSuccess){
                                UserInfoContext.mUserAddress.set(position,userAddress);
                            }
                        }else{
                            userAddress = (UserAddress) MyPost.doPost("addUserAddress",userAddress);
                            // 保存地址
                            // 新增收货地址的时候，再判断一下 UserInfoContext 是否为空 ，这样也好设置一下 默认的收货地址
                            Boolean flag = false;
                            if(UserInfoContext.mUserAddress == null || UserInfoContext.mUserAddress.size() == 0){
                                flag = true;
                                UserInfoContext.mUserAddress = new ArrayList<>();
                            }
                            UserInfoContext.mUserAddress.add(userAddress);
                            if(flag){
                                UserInfoContext.userReceiveAddr = userAddress;
                            }

                        }
                    }
                });
                // 返回到收货地址那里
                Intent intent = new Intent();
                intent.setComponent(new ComponentName(addAddress.this, AddressActivity.class));
                //intent.putExtra("fromActivity","add_update_addr");
                addAddress.this.startActivity(intent);
                addAddress.this.finish();
            }
        });

    }

    private void showAddr(){
        showAddr.setVisibility(View.VISIBLE);
        selectAddr.setVisibility(View.GONE);
    }

    private void showUpdateAddr(){
        showAddr.setVisibility(View.GONE);
        selectAddr.setVisibility(View.VISIBLE);
    }

    // 跳转activity返回结果
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        if(requestCode == 1){
            if(resultCode == 1){
                // 将返回的结果 设置到 收货地址上
                Bundle bundle = data.getExtras();
                UserAddress userAddress1 = (UserAddress) bundle.get("UserAddress");
                if(userAddress != null &&userAddress.getId() != null){
                    // 只需要获取经纬度坐标和 address即可
                    userAddress.setAddress(userAddress1.getAddress());
                    userAddress.setLatitude(userAddress1.getLatitude());
                    userAddress.setLongitude(userAddress1.getLongitude());
                }else{
                    userAddress = userAddress1;
                }
                // 设置一下信息
                showAddr();
                addrAddress.setText(userAddress.getAddress());
            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
}